import json
import time
import math
import random
import threading
from weibosender.weibo.weibo import Weibo
from weibosender.dal.receivers_dal import ReceiversDAL
from weibosender.dal.tags_dal import TagsDAL
from weibosender.config.gather_page_record_config import GatherPageConfig


class WeiboGatherHandler(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.stopped = False

    def run(self):
        tags = TagsDAL.get_tags()
        if not tags:
            return None
        tags_count = len(tags)
        current_page_config = GatherPageConfig.get_current_gather_page()
        tag_id = None
        tag_name = None
        current_page = 1
        if current_page_config:
            tag_id = current_page_config['id']
            tag_name = current_page_config['name']
            current_page = current_page_config['page']

        if tag_id and tag_name:
            print('从上次采集处"%s"，第%s页开始继续采集' % (tag_name, current_page))

        index = 0
        for i in range(tags_count):
            if tags[i][1] == tag_id:
                index = i
                break;

        index = index > 0 and index or 0

        for i in range(index, tags_count):
            tag = {
                'id': tags[i][1],
                'name': tags[i][2]
            }
            self._handle(tag, current_page)
            current_page = 1

        if not self.stopped and i == len(tags):
            GatherPageConfig.write('')
            print('Overall > done []~(￣▽￣)~*')

    '''
    采集用户数据
    '''
    def _handle(self, tag, page=1):
        if self.stopped: return False
        tag_id = tag['id']
        tag_name = tag['name']

        print('正在采集"%s"用户 > 第%s页' % (tag_name, page))

        card_list = Weibo.get_user(tag_id, page)
        if not card_list:
            WeiboGatherHandler._delay_gather('采集异常 > 未获取到数据，可能被目标服务器发现了，让我先休息%s秒zzz')
            return self._handle(tag, page)

        try:
            json_card_list = json.loads(card_list)
            if json_card_list['ok'] != 1:
                WeiboGatherHandler._delay_gather('采集异常 > 访问过于频繁，让我先休息%s秒zzz')
                return self._handle(tag, page)

            card_list_info = json_card_list['cardlistInfo']
            total = 0
            if card_list_info:
                total = card_list_info['total']
            page_count = math.ceil(total / 10)

            cards = json_card_list['cards']
            cards_count = len(cards)
            card_group_index = -1
            for i in range(cards_count):
                if 'card_group' in cards[i]:
                    card_group_index = i
                    break

            if card_group_index == -1:
                WeiboGatherHandler._delay_gather('采集异常 > 未找到用户信息，将在%s秒后重试zzz')
                return self._handle(tag, page)

            card_group = cards[card_group_index]['card_group']

            WeiboGatherHandler._storage_user(tag, card_group)

            page += 1
            if page < page_count:
                GatherPageConfig.write('{\n\t"id": "%s",\n\t"name": "%s",\n\t"page":%d\n}' % (tag_id, tag_name, page))
                return self._handle(tag, page)
            else:
                print('采集"%s"数据 > done...\n' % tag_name)
        except Exception as e:
            card_list = card_list is None and 'None' or card_list
            print('采集异常：%s\n%s' % (e, card_list))
            WeiboGatherHandler._delay_gather('将在%s秒后重试zzz')
            return self._handle(tag, page)

        return True

    def stop(self):
        self.stopped = True

    @staticmethod
    def _delay_gather(tip_text):
        delay = random.randint(5, 10)
        print(tip_text % delay)
        time.sleep(delay)

    '''
    采集用户数据入库
    '''
    @staticmethod
    def _storage_user(tag, users, index=0):
        user = users[index]['user']
        data = (user['id'], user['screen_name'], user['gender'], tag['id'], tag['name'])

        # 入库
        ReceiversDAL.append(data)

        index += 1
        if index < len(users):
            WeiboGatherHandler._storage_user(tag, users, index)